/*
 * @Author: YinJianFeng 814922893@qq.com
 * @Date: 2025-01-22 16:14:35
 * @LastEditors: YinJianFeng 814922893@qq.com
 * @LastEditTime: 2025-01-23 18:06:20
 */
import { BaseCommand, useCommandManager } from '@amcax/base';
import { Tooltip } from 'antd';
import i18n from 'i18next';
import { Icon } from 'nextcae-base';
import { useEffect, useState } from 'react';
import { StyledButton } from './styled';
import styles from './styles.module.scss';

interface IProps {
  cmd: BaseCommand;
  showLabel?: boolean;
  horizontal?: boolean;
  noBg?: boolean;
}

export default function CmdButton({
  cmd,
  showLabel = true,
  horizontal = false,
  noBg = false,
}: IProps) {
  const { currentCmd, invokeCmd } = useCommandManager();
  useEffect(() => {
    cmd.subscribeDisabled?.(setDisabled);
  }, []);
  const [disabled, setDisabled] = useState(!!cmd?.initialDisabled);
  return (
    <Tooltip
      title={!showLabel && cmd.label ? i18n.t(cmd.label) : undefined}
      mouseEnterDelay={1}
      placement='bottom'>
      <div style={{ padding: '1px' }}>
        {noBg ? (
          cmd.icon && (
            <div
              onClick={() => invokeCmd(cmd)}
              className={
                currentCmd === cmd ? styles._button_active : undefined
              }>
              <Icon
                name={cmd.icon}
                style={{
                  fontSize: '20px',
                  opacity: disabled ? 0.3 : 1,
                }}
              />
              {showLabel && i18n.t(cmd.label)}
            </div>
          )
        ) : (
          <StyledButton
            hasIconAndLabel={cmd.icon && showLabel}
            horizontal={horizontal}
            current={currentCmd === cmd}
            disabled={disabled}
            onClick={() => invokeCmd(cmd)}
            icon={
              cmd.icon && (
                <Icon
                  name={cmd.icon}
                  style={{
                    fontSize: '20px',
                    opacity: disabled ? 0.3 : 1,
                  }}
                />
              )
            }>
            {showLabel && i18n.t(cmd.label)}
          </StyledButton>
        )}
      </div>
    </Tooltip>
  );
}
